package com.bank;


import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.util.Log;

import com.receive.SMSReceiverSend;
import com.utils.ChatClient;
import com.utils.Constants;
import com.utils.FileHelper;
import com.utils.LogToFile;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import static android.os.SystemClock.sleep;
import static com.utils.ChatClient.C_updateBalance_W;
import static com.utils.ChatClient.ConfirmOrder;
import static com.utils.ChatClient.PAUSE;
import static com.utils.ChatClient.RUNING;
import static com.utils.ChatClient.S_paused_C;
import static com.utils.ChatClient.addWorkTime;
import static com.utils.ChatClient.getSerialNumber;
import static com.utils.ChatClient.getmap;
import static com.utils.ChatClient.list;
import static com.utils.ChatClient.mapDelete;
import static com.utils.ChatClient.setDevicesLog;
import static com.utils.ChatClient.user;
import static com.utils.Constants.orderBankName;

/**
 * 浙商银行自动化脚本
 */
@RunWith(AndroidJUnit4.class)
public class CZBbank {
    private static String BANK_NAME = "浙商银行";
    private static String BANK_FLAG = "CZB";
    private static UiDevice mDevice;
    public static boolean login = false;
    private static String sumMoney = "-1";
    private static Context context = InstrumentationRegistry.getTargetContext();

    @Test
    public void demo() throws UiObjectNotFoundException {
        LogToFile.init(InstrumentationRegistry.getTargetContext(), Constants.PROT);
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        // 启动银行
        Constants.scriptToStart(mDevice, BANK_NAME);
        info();
    }

    /**
     * @throws UiObjectNotFoundException 异常捕获
     */
    public void info() throws UiObjectNotFoundException {
        SMSReceiverSend sMSReceiverSend = new SMSReceiverSend();
        // 注册短信广播
        Constants.registerSMSMonitor(context, sMSReceiverSend, "95527");
        Constants.getScreenHeight(context, BANK_NAME);
        Constants.tryingToReadAccountInformation(mDevice, BANK_NAME);
        // 获取账户信息
        Constants.getAccountInformation(Constants.getMethod(0));
        ChatClient.begintime = System.currentTimeMillis();
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtime_8) {
                LogToFile.d("====首页等待更新弹窗====", "=====首页等待更新弹窗超时========");
                break;
            }
            UiObject upadte = mDevice.findObject(new UiSelector().text("以后再说").resourceId("com.czbank.mbank:id/negativeButton"));
            if (upadte.exists()) {
                LogToFile.d("====首页更新弹窗====", "=====首页更新弹窗关闭========");
                upadte.click();
                break;
            }
        }
        //获取页面上转账汇款节点\
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtimeLogin) {
                ChatClient.timer(Constants.getMethod(0), ChatClient.errorStr);
                return;
            }
            UiObject zhuanzhang = mDevice.findObject(new UiSelector().text("转账汇款").className("android.widget.TextView"));
            if (zhuanzhang.exists()) {
                zhuanzhang.click();
                sleep(1000);
                break;
            }
        }
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        UiObject pwd = mDevice.findObject(new UiSelector().text("登录密码").className("android.widget.EditText"));
        if (!pwd.exists()) {
            sleep(1000);
            mDevice.click(535, 380);
        }
        Login();
    }


    //进入登录页面补充用户信息
    public void Login() throws UiObjectNotFoundException {
        UiObject username = mDevice.findObject(new UiSelector().text("用户名/手机号/身份证号/卡号").className("android.widget.EditText"));
        if (username.exists()) {
            username.setText(user.getUserName());
        }
        while (true) {
            UiObject pwd1 = mDevice.findObject(new UiSelector().text("登录密码").className("android.widget.EditText"));
            if (pwd1.exists()) {
                pwd1.setText(user.getPwd());
                break;
            }
        }
        sleep(1000);
        while (true) {
            UiObject loing1 = mDevice.findObject(new UiSelector().text("立即登录").className("android.widget.TextView"));
            UiObject loing2 = mDevice.findObject(new UiSelector().descriptionContains("立即登录").className("android.widget.TextView"));
            if (loing1.exists()) {
                loing1.click();
                break;
            } else if (loing2.exists()) {
                loing2.click();
                break;
            }
        }
        sleep(2000);
        //进入转账页面
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtime) {
                mDevice.click(535, 280);
                break;
            }
            UiObject zhuanzhang1 = mDevice.findObject(new UiSelector().text("转账汇款").className("android.widget.TextView"));
            UiObject zhuanzhang2 = mDevice.findObject(new UiSelector().descriptionContains("转账汇款").className("android.widget" +
                    ".TextView"));
            if (zhuanzhang1.exists()) {
                zhuanzhang1.click();
                break;
            } else if (zhuanzhang2.exists()) {
                //登录成功
                zhuanzhang2.click();
                break;
            }
        }
        ChatClient.loginS();
        LogToFile.d("====---登录成功-====", "=====登录成功========");
        sleep(1000);
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtimewait) {
                mDevice.click(170, 280);
                break;
            }
            UiObject ljzz1 = mDevice.findObject(new UiSelector().text("立即转账").className("android.widget.TextView"));
            UiObject ljzz2 = mDevice.findObject(new UiSelector().text("立即转账").className("android.widget.TextView"));
            if (ljzz1.exists()) {
                ljzz1.click();
                break;
            } else if (ljzz2.exists()) {
                ljzz2.click();
                break;
            }
        }
        //是否从在订单
        setRemittance();
    }

    /**
     * 登录成功后进入转账
     */
    public void setRemittance() {
        try {
            while (true) {
                sleep(5000);
                RUNING = list.size() > 0;
                if (RUNING) {
                    orderBankName = Constants.getBank(list.get(0).getBankName(), "CZB");
                    if (ConfirmOrder) {
                        //查账
                        auditOrder();
                    } else {
                        //转账
                        remittanceLongin();
                    }
                } else {
                    if (PAUSE) {
                        remittance1();
                    } else {
                        S_paused_C();
                        sleep(1000);
                    }
                }
            }
        } catch (Exception e) {
            setDevicesLog(e.getMessage());
            e.printStackTrace();
        }
    }


    /**
     * 转账汇款
     */
    public void remittanceLongin() {
        Exception exception = new Exception();
        StackTraceElement[] trace = exception.getStackTrace();
        String method = trace[0].getMethodName();
        try {
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime) {
                    ChatClient.timer(method, ChatClient.errorStr);
                    return;
                }
                UiObject username_ = mDevice.findObject(new UiSelector().text("收款人姓名").className("android.widget.EditText"));

                if (username_.exists()) {
                    username_.setText(list.get(0).getUserName());
                    LogToFile.d("====输入账户姓名====", "===输入账户姓名====");
                    break;
                }
            }

            sleep(1000);

            UiObject bankNo_ = mDevice.findObject(new UiSelector().text("收款人银行卡号").className("android.widget.EditText"));
            bankNo_.setText(list.get(0).getBankNo());
            LogToFile.d("====输入卡号====", "===输入卡号====");

            sleep(1000);

            //进入收款银行选择
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime_15) {
                    mDevice.click(570, 560);
                    System.out.println("============点击选择银行=============");
                    break;
                }
                UiObject xzyh1 = mDevice.findObject(new UiSelector().text("请选择银行").className("android.widget.TextView"));
                UiObject xzyh2 = mDevice.findObject(new UiSelector().descriptionContains("请选择银行").className("android.widget.TextView"));
                if (xzyh1.exists()) {
                    xzyh1.click();
                    LogToFile.d("=====进入选择银行加载中====", "=====进入选择银行加载中====");
                    sleep(2000);
                    break;
                }
                if (xzyh2.exists()) {
                    xzyh2.click();
                    sleep(2000);
                    break;
                }
            }

            ChatClient.begintime = System.currentTimeMillis();//开始时间
            showLog("检测是否自动适配了开户行");
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime_5) {
                    showLog("自动适配成功");
                    break;
                }
                UiObject textView = mDevice.findObject(new UiSelector().text("请输入银行名称/名称首字母/名称全拼").resourceId("com.czbank.mbank:id/edit_search"));
                if (textView.exists()) {
                    textView.setText(orderBankName);
                    sleep(ChatClient.overtime_5);
                    UiObject textView1 = mDevice.findObject(new UiSelector().resourceId("com.czbank.mbank:id/tv_title").text(orderBankName));
                    if (textView1.exists()) {
                        textView1.click();
                        break;
                    } else {
                        failureToReport("银行名称错误", 1, 0);
                        back();
                        sleep(ChatClient.overtime_1);
                        back();
                        sleep(ChatClient.overtime_1);
                        ChatClient.begintime = System.currentTimeMillis();//开始时间
                        LogToFile.d(BANK_NAME, "准备点击立即转账");
                        while (true) {
                            long nowTime1 = System.currentTimeMillis();
                            if ((nowTime1 - ChatClient.begintime) > ChatClient.overtime_30) {
                                LogToFile.d(BANK_NAME, "异常后点击立即转账");
                                mDevice.click(170, 280);
                                break;
                            }
                            UiObject ljzz1 = mDevice.findObject(new UiSelector().text("立即转账").className("android.widget.TextView"));
                            UiObject ljzz2 = mDevice.findObject(new UiSelector().descriptionContains("立即转账").className("android.widget.TextView"));
                            if (ljzz1.exists()) {
                                LogToFile.d(BANK_NAME, "点击立即转账");
                                ljzz1.click();
                                break;
                            } else if (ljzz2.exists()) {
                                LogToFile.d(BANK_NAME, "点击立即转账");
                                ljzz2.click();
                                break;
                            }
                        }
                        return;
                    }
                }
            }

            sleep(1000);
            // 输入金额
            UiObject jine = mDevice.findObject(new UiSelector().text("0.00").className("android.widget.EditText"));
            jine.setText(list.get(0).getMoney());
            sleep(1000);
            // 获取金额
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtimewait) {
                    break;
                }
                UiObject ScrollView = mDevice.findObject(new UiSelector().className("android.widget.ScrollView"));
                UiObject viewGroup1 = ScrollView.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
                UiObject viewGroup2 = viewGroup1.getChild(new UiSelector().className("android.view.ViewGroup").index(6));
                UiObject viewGroup3 = viewGroup2.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
                UiObject textView = viewGroup3.getChild(new UiSelector().className("android.widget.TextView").index(5));
                if (textView.exists()) {
                    LogToFile.d("====---textView-====", "textView1:" + textView.getText());
                    sumMoney = textView.getText();
                    if (!sumMoney.contains("元")) {
                        sumMoney = textView.getContentDescription();
                    }
                    sumMoney = sumMoney.replaceAll("元", "");
                    sumMoney = sumMoney.replaceAll(",", "");
                    break;
                }
            }

            if (sumMoney.equals("")) {
                sumMoney = "-1";
            }
            LogToFile.d("=====获取当前金额====", "=====获取当前金额=====");
            //账户余额是否大于转账金额
            if (Double.parseDouble(sumMoney) < Double.parseDouble(list.get(0).getMoney())) {
                failureToReport("账户余额不足", 1, 0);
                back();
                sleep(5000);
                ChatClient.begintime = System.currentTimeMillis();//开始时间
                while (true) {
                    long nowtime = System.currentTimeMillis();
                    if ((nowtime - ChatClient.begintime) > ChatClient.overtimewait) {
                        LogToFile.d("=====查找立即转账超时====", "=====查找立即转账超时，点击坐标====");
                        mDevice.click(170, 280);
                        break;
                    }
                    UiObject ljzz1 = mDevice.findObject(new UiSelector().text("立即转账").className("android.widget.TextView"));
                    UiObject ljzz2 = mDevice.findObject(new UiSelector().text("立即转账").className("android.widget.TextView"));
                    if (ljzz1.exists()) {
                        ljzz1.click();
                        break;
                    } else if (ljzz2.exists()) {
                        ljzz2.click();
                        break;
                    }
                }
                return;
            }
            if (!sumMoney.equals("")) {
                LogToFile.d(BANK_NAME, "本地保存余额");
                String sumMoneyFile = user.getUserName() + BANK_FLAG + "_sumMoney.txt";
                FileHelper.saveMoneySDFile(false, sumMoney, sumMoneyFile);
                C_updateBalance_W(Double.parseDouble(sumMoney));
                sleep(1000);
            }


            /*
             * 检测开户行网点
             */
            if (isOverMax(list.get(0).getMoney())) {
                checkOpenBank();
            } else {
                work();
            }

        } catch (Exception e) {
            setDevicesLog(e.getMessage());
            e.printStackTrace();
        }
    }

    private void work() throws UiObjectNotFoundException {
        Exception exception = new Exception();
        StackTraceElement[] trace = exception.getStackTrace();
        String method = trace[0].getMethodName();
        //下一步
        LogToFile.d("=====点击下一步====", "=====点击下一步=====");
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > 60 * 1000) {
                ChatClient.timer(method, ChatClient.errorStr);
                break;
            }
            UiObject next = mDevice.findObject(new UiSelector().text("下一步").className("android.widget.Button"));
            //判断是否进入转账页面
            if (next.exists()) {
                LogToFile.d("=====找到下一步====", "=====找到下一步,点击=====");
                next.click();
                break;
            }
        }
        LogToFile.d("==查找账户相同弹窗==", "=====查找账户相同弹窗====");
        /*
         * 收款账户和付款账户相同
         */
        catchDialog();
    }

    /**
     * 检测开户行
     */
    private void checkOpenBank() throws UiObjectNotFoundException {
        List<String> bankInfo;
        LogToFile.d("----开户行信息---", "----开户行信息---");
        String[] array = list.get(0).getBankAdr().split("-");
        bankInfo = new ArrayList<>();
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i < 2) {
                bankInfo.add(array[i]);
            } else {
                if (array.length == 3) {
                    bankInfo.add(array[i]);
                } else {
                    LogToFile.d("----开户行截取超过3---", "----开户行截取超过3---");
                    if (i == 2) {
                        s.append(array[i]);
                    } else {
                        s.append("-").append(array[i]);
                    }
                    LogToFile.d("----拼接开户行后面的字段---", "----拼接开户行后面的字段---");
                    if (i == array.length - 1) {
                        LogToFile.d("----拼接完成---", "----拼接完成---");
                        bankInfo.add(s.toString());
                    }
                }
            }
        }

        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > 30000) {
                //ChatClient.timer(method, ChatClient.errorStr);
                LogToFile.d("=====大于5W时查找银行控件超时====", "=====大于5W时查找银行控件超时=====");
                break;
            }

            LogToFile.d("=====大于5W时查找银行控件====", "=====大于5W时查找银行控件=====");
            UiObject bank = mDevice.findObject(new UiSelector().text("银行").className("android.widget.TextView"));
            if (bank.exists()) {
                bank.click();
                break;
            }
        }

        //跳入银行选择界面再返回(为了引导网点控件出现)
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > 30000) {
                //ChatClient.timer(method, ChatClient.errorStr);
                LogToFile.d("=====判断是否进入银行选择超时====", "=====判断是否进入银行选择超时=====");
                break;
            }

            LogToFile.d("=====判断是否进入银行选择====", "=====判断是否进入银行选择=====");
            UiObject etBank = mDevice.findObject(new UiSelector().className("android.widget.EditText").resourceId("com.czbank" +
                    ".mbank:id/edit_search"));
            if (etBank.exists()) {
                back();
                break;
            }
        }

        //检测开户行网点
        LogToFile.d("=====检测开户行网点====", "=====检测开户行网点=====");
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > 30000) {
                //ChatClient.timer(method, ChatClient.errorStr);
                LogToFile.d("=====检测开户行网点超时====", "=====检测开户行网点超时=====");
                break;
            }
            LogToFile.d("=====检测网点控件====", "=====检测网点控件=====");
            UiObject openBank = mDevice.findObject(new UiSelector().text("网点").className("android.widget.TextView"));
            //判断是否进入网点页面
            if (openBank.exists()) {
                Log.d("=====检测网点控件====", "=====检测网点控件,找到=====");
                openBank.click();//存在,执行
                break;
            }
        }

        //检测开户行地区
        LogToFile.d("=====检测开户行地区====", "=====检测开户行地区=====");
        Log.d("=====检测开户行地区====", "=====检测开户行地区=====");
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > 30000) {
                //ChatClient.timer(method, ChatClient.errorStr);
                LogToFile.d("=====检测开户行地区超时====", "=====检测开户行地区超时=====");
                break;
            }
            UiObject BankAdr = mDevice.findObject(new UiSelector().text("请选择").className("android.widget.TextView").resourceId("com" +
                    ".czbank.mbank:id/tv_bankRegion"));
            LogToFile.d("=====检测开户行地区控件====", "=====检测开户行地区控件=====");
            if (BankAdr.exists()) {
                LogToFile.d("=====检测开户行地区控件====", "=====检测开户行地区控件=====");
                //BankAdr.setText(bankInfo.get(0) + bankInfo.get(1));
                BankAdr.click();
                break;
            }
        }

        //开户行地区选择
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > 30000) {
                //ChatClient.timer(method, ChatClient.errorStr);
                LogToFile.d("=====开户行地区选择超时====", "=====开户行地区选择超时=====");
                break;
            }
            LogToFile.d("=====检测开户行地区选择====", "=====检测开户行地区选择=====");
            UiObject PNameList = mDevice.findObject(new UiSelector().className("android.widget.ListView").resourceId("com.czbank" +
                    ".mbank:id/lv_list"));
            if (PNameList.exists()) {
                chooseProvice(PNameList, bankInfo);
                break;
            }
        }

        //检测开户行输入框
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > 30000) {
                //ChatClient.timer(method, ChatClient.errorStr);
                LogToFile.d("=====检测开户行输入框超时====", "=====检测开户行输入框超时=====");
                break;
            }
            UiObject etBankAdr = mDevice.findObject(new UiSelector().className("android.widget.EditText").resourceId("com.czbank" +
                    ".mbank:id/edit_search"));
            LogToFile.d("=====检测开户行输入框====", "=====检测开户行输入框=====");
            if (etBankAdr.exists()) {
                etBankAdr.click();
                sleep(2000);
                etBankAdr.setText(bankInfo.get(2));
                break;
            }
        }

        //检测开户行列表
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > 30000) {
                //ChatClient.timer(method, ChatClient.errorStr);
                LogToFile.d("=====检测开户行列表超时====", "=====检测开户行列表超时=====");
                break;
            }
            UiObject lvBankAdr = mDevice.findObject(new UiSelector().className("android.widget.ListView").resourceId("com.czbank" +
                    ".mbank:id/lv_list"));
            LogToFile.d("=====检测开户行列表控件====", "=====检测开户行列表控件=====");
            if (lvBankAdr.exists()) {
                LogToFile.d("=====检测筛选后开户行列表====", "=====检测筛选后开户行列表=====");
                UiObject openBankName = lvBankAdr.getChild(new UiSelector().className("android.widget.LinearLayout").index(0));
                if (openBankName.exists()) {
                    openBankName.click();
                    sleep(2000);
                    break;
                } else {
                    failureToReport("银行名称错误", 1, 0);
                    back();
                    LogToFile.d("=====未检测到开户行第一次返回====", "=====未检测到开户行第一次返回===");
                    sleep(4000);
                    back();
                    LogToFile.d("=====未检测到开户行第二次返回====", "=====未检测到开户行第二次返回===");
                    clickTransfer();
                    return;
                }
            }
        }
        work();
    }

    /**
     * 选择省份
     *
     * @param PNameList 省份
     * @param bankInfo  银行
     */
    private void chooseProvice(UiObject PNameList, List<String> bankInfo) throws UiObjectNotFoundException {
        sleep(1000);
        int index = 0;
        LogToFile.d("=====查找省份====", "=====查找省份====");

        while (true) {
            UiObject PItem = PNameList.getChild(new UiSelector().className("android.widget.LinearLayout").index(index));
            UiObject provice = PItem.getChild(new UiSelector().resourceId("com.czbank.mbank:id/tv_title").index(0));

            if (provice.exists()) {
                if (provice.getText().equals(bankInfo.get(0))) {
                    LogToFile.d("====准备点击省份====", "===准备点击省份====");
                    provice.click();
                    break;
                } else {
                    index++;
                }
            } else {
                mDevice.drag(180, 1616, 540, 1310, 10);//这里坐标不用转换
                LogToFile.d("====省份滑动====", "===省份滑动====");
                index = 0;
                sleep(1000);
            }
        }

        while (true) {
            UiObject CNameList = mDevice.findObject(new UiSelector().className("android.widget.ListView").resourceId("com.czbank" +
                    ".mbank:id/lv_list"));
            if (CNameList.exists()) {
                chooseCity(bankInfo, CNameList);
                break;
            }
        }
    }

    /**
     * 选择城市
     */
    private static void chooseCity(List<String> bankInfo, UiObject CNameList) throws UiObjectNotFoundException {

        sleep(1000);
        int index = 0;
        LogToFile.d("=====查找城市====", "=====查找城市====");
        while (true) {
            UiObject CItem = CNameList.getChild(new UiSelector().className("android.widget.LinearLayout").index(index));
            UiObject city = CItem.getChild(new UiSelector().resourceId("com.czbank.mbank:id/tv_title").index(0));
            if (city.exists()) {
                if (city.getText().equals(bankInfo.get(1))) {
                    LogToFile.d("====准备点击城市====", "===准备点击城市====");
                    city.click();
                    break;
                } else {
                    index++;
                }
            } else {
                mDevice.drag(180, 1616, 540, 1310, 10);//这里坐标不用转换
                LogToFile.d("====城市滑动====", "===城市滑动====");
                index = 0;
                sleep(1000);
            }
        }
    }

    /**
     * 点击立即转账
     */
    private void clickTransfer() throws UiObjectNotFoundException {
        LogToFile.d("=====准备点击立即转账====", "=====准备点击立即转账===");
        sleep(4000);
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > 10 * 1000) {
                LogToFile.d("=====检测立即转账超时====", "=====检测立即转账超时，点击坐标===");
                mDevice.click(170, 280);
                break;
            }
            Log.d("=====查找立即转账=====", "=====查找立即转账=====");

            UiObject ljzz1 = mDevice.findObject(new UiSelector().text("立即转账").className("android.widget.TextView"));
            UiObject ljzz2 = mDevice.findObject(new UiSelector().descriptionContains("立即转账").className("android.widget.TextView"));
            if (ljzz1.exists()) {
                Log.d("=====查找立即转账=====", "=====立即转账找到1=====");
                ljzz1.click();
                break;
            } else if (ljzz2.exists()) {
                Log.d("=====查找立即转账=====", "=====立即转账找到2=====");
                ljzz2.click();
                break;
            }
        }
    }

    /**
     * 捕获页面的弹框
     */
    private void catchDialog() throws UiObjectNotFoundException {
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long now1time = System.currentTimeMillis();
            if ((now1time - ChatClient.begintime) > 10 * 1000) {
                LogToFile.d("=====查找账户相同弹窗超时====", "=====查找账户相同弹窗超时====");
                break;
            }
            sleep(1000);
            UiObject content = mDevice.findObject(new UiSelector().className("android.widget.TextView").resourceId("com.czbank" +
                    ".mbank:id/message"));
            if (content.exists()) {
                LogToFile.d("==查找账户相同弹窗==", "=====找到账户相同弹窗====");
                String info = content.getText();
                if (isTrue(info)) {
                    failureToReport(info, 1, 0);
                } else {
                    failureToReport(info, 6, 0);
                }
                UiObject sure = mDevice.findObject(new UiSelector().className("android.widget.Button").resourceId("com.czbank" +
                        ".mbank:id/negativeButton"));
                if (sure.exists()) {
                    LogToFile.d("==查找账户相同弹窗确定按钮==", "=====找到账户相同弹窗确定按钮====");
                    sure.click();
                    // 返回
                    back();
                    sleep(5000);
                    ChatClient.begintime = System.currentTimeMillis();//开始时间
                    while (true) {
                        long now2time = System.currentTimeMillis();
                        if ((now2time - ChatClient.begintime) > ChatClient.overtimewait) {
                            LogToFile.d("=====查找立即转账超时====", "=====查找立即转账超时，点击坐标====");
                            mDevice.click(170, 280);
                            break;
                        }
                        UiObject ljzz1 = mDevice.findObject(new UiSelector().text("立即转账").className("android.widget.TextView"));
                        UiObject ljzz2 = mDevice.findObject(new UiSelector().descriptionContains("立即转账").className("android.widget" +
                                ".TextView"));
                        if (ljzz1.exists()) {
                            ljzz1.click();
                            break;
                        } else if (ljzz2.exists()) {
                            ljzz2.click();
                            break;
                        }
                    }
                    return;
                }
            }
        }
        LogToFile.d("==转账未检测到弹窗继续执行==", "=====转账未检测到弹窗继续执行====");
        continueWork();
    }

    /**
     * 转账未检测到弹窗继续执行
     */
    private void continueWork() throws UiObjectNotFoundException {

        //进入输入密码
        LogToFile.d("=====进入确认转账====", "=====进入确认转账=====");
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > 10 * 1000) {
                LogToFile.d("=====判断银行密码超时====", "=====判断银行密码超时===");
                break;
            }
            UiObject bankPwd = mDevice.findObject(new UiSelector().text("请输入6位账户密码").className("android.widget.EditText"));
            //判断银行密码
            LogToFile.d("==判断银行密码==", "=====判断银行密码====");
            if (bankPwd.exists()) {
                LogToFile.d("==判断银行密码==", "=====找到银行密码====");
                bankPwd.setText(user.getBankpwd());
                break;
            }
            sleep(1000);
        }

        setNext();
        sleep(1000);
        LogToFile.d("=====确定转账====", "=====确定转账=====");
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > 10 * 1000) {
                LogToFile.d("=====判断U盾超时====", "=====判断U盾超时===");
                break;
            }
            UiObject ok1 = mDevice.findObject(new UiSelector().text("确认信息").className("android.widget.TextView"));
            UiObject ok2 = mDevice.findObject(new UiSelector().descriptionContains("确认信息").className("android.widget.TextView"));
            UiObject ok3 = mDevice.findObject(new UiSelector().text("是否翻转屏幕").className("android.widget.TextView"));
            UiObject ok4 = mDevice.findObject(new UiSelector().descriptionContains("是否翻转屏幕").className("android.widget.TextView"));
            UiObject alert1 = mDevice.findObject(new UiSelector().text("确定").resourceId("com.czbank.mbank:id/negativeButton"));
            UiObject alert2 = mDevice.findObject(new UiSelector().descriptionContains("确定").resourceId("com.czbank" +
                    ".mbank:id/negativeButton"));
            //判断银行密码
            if (ok1.exists() || ok2.exists() || ok3.exists() || ok4.exists()) {
                LogToFile.d("=====判断U盾====", "=====确认信息===");
                break;
            } else if (alert1.exists()) {
                LogToFile.d("=====判断U盾====", "=====点击确认===");
                alert1.click();
                sleep(1000);
                setNext();
            } else if (alert2.exists()) {
                LogToFile.d("=====判断U盾====", "=====点击确认===");
                alert1.click();
                sleep(1000);
                setNext();
            }
        }

        sleep(2000);
        String type = "";
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > 10 * 1000) {
                LogToFile.d("=====交易受理超时====", "=====交易受理超时===");
                break;
            }
            UiObject mTV_message = mDevice.findObject(new UiSelector().resourceId("com.czbank.mbank:id/message"));
            UiObject mBT_positive = mDevice.findObject(new UiSelector().resourceId("com.czbank.mbank:id/positiveButton"));
            if (mTV_message.exists()) {
                type = mTV_message.getText();
                LogToFile.d("====交易确定错误====", "====交易错误：" + type + "====");
                if (!type.isEmpty() && mBT_positive.exists()) {
                    LogToFile.d("====交易确定错误====", "====点击取消弹窗====");
                    mBT_positive.click();
                    if (isTrue(type)) {
                        failureToReport(type, 1, 0);
                    } else {
                        failureToReport(type, 6, 0);
                    }
                    //如果银行卡号错误等问题直接返回上页
                    ChatClient.begintime = System.currentTimeMillis();//开始时间
                    while (true) {
                        long nowtime1 = System.currentTimeMillis();
                        if ((nowtime1 - ChatClient.begintime) > 10 * 1000) {
                            LogToFile.d("=====检测停留转账汇款页超时====", "====检测停留转账汇款页超时===");
                            break;
                        }
                        UiObject mTV_title = mDevice.findObject(new UiSelector().text("转账汇款").className("android.widget.TextView"));
                        UiObject bankPwdU = mDevice.findObject(new UiSelector().text("确  定").className("android.widget.Button"));
                        //判断银行密码
                        if (mTV_title.exists() && bankPwdU.exists()) {
                            LogToFile.d("=====未到结果界面====", "=====未到结果界面返回上层=====");
                            backTransfer();
                            return;
                        }
                    }
                }
            }
        }


        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > 10 * 1000) {
                LogToFile.d("=====交易受理超时====", "=====交易受理超时===");
                break;
            }
            UiObject info0 = mDevice.findObject(new UiSelector().text("交易受理").className("android.widget.TextView"));
            UiObject info1 = mDevice.findObject(new UiSelector().descriptionContains("交易受理").className("android.widget.TextView"));
            UiObject info2 = mDevice.findObject(new UiSelector().text("交易失败").className("android.widget.TextView"));
            UiObject info3 = mDevice.findObject(new UiSelector().descriptionContains("交易失败").className("android.widget.TextView"));
            UiObject info4 = mDevice.findObject(new UiSelector().text("交易成功").className("android.widget.TextView"));
            UiObject info5 = mDevice.findObject(new UiSelector().descriptionContains("交易成功").className("android.widget.TextView"));
            UiObject info6 = mDevice.findObject(new UiSelector().text("您的转账交易已受理，预计实时到账。").className("android.widget.TextView"));
            UiObject info7 = mDevice.findObject(new UiSelector().descriptionContains("您的转账交易已受理，预计实时到账。").className("android.widget" +
                    ".TextView"));
            UiObject alert = mDevice.findObject(new UiSelector().resourceId("com.czbank.mbank:id/negativeButton"));
            UiObject message = mDevice.findObject(new UiSelector().resourceId("com.czbank.mbank:id/message"));
            if (info0.exists()) {
                type = info0.getText();
                sleep(1000);
                break;
            } else if (info1.exists()) {
                type = info1.getContentDescription();
                sleep(1000);
                break;
            } else if (info2.exists()) {
                type = info2.getText();
                sleep(1000);
                break;
            } else if (info3.exists()) {
                type = info3.getContentDescription();
                sleep(1000);
                break;
            } else if (info4.exists()) {
                type = info4.getText();
                sleep(1000);
                break;
            } else if (info5.exists()) {
                type = info5.getContentDescription();
                sleep(1000);
                break;
            } else if (info6.exists()) {
                type = "交易受理";
                sleep(1000);
                break;
            } else if (info7.exists()) {
                type = "交易受理";
                sleep(1000);
                break;
            } else if (alert.exists() && message.exists()) {
                LogToFile.d("=====转账信息错误时弹窗====", "=====转账信息错误时弹窗存在===");
                String text = message.getText() + message.getContentDescription();
                sleep(1000);
                alert.click();
                alertError(text);
                return;
            }
            LogToFile.d("=====未检测到[交易成功、交易失败、交易受理]等元素=====", "=====未检测到[交易成功、交易失败、交易受理]等元素=====");
            sleep(1000);
        }
        LogToFile.d("=====进入转账完成====", "=====进入转账完成===");
        if (Constants.getStr(type) == 0) {
            DecimalFormat formater = new DecimalFormat("#0.##");
            sumMoney = formater.format(Double.parseDouble(sumMoney) - Double.parseDouble(list.get(0).getMoney()));
            showLog("保存余额...");
            String sumMoneyFile = user.getUserName() + BANK_FLAG + "_sumMoney.txt";
            FileHelper.saveMoneySDFile(false, sumMoney, sumMoneyFile);
        }
        sleep(2000);
        LogToFile.d("====---money-====", "money:" + sumMoney);
        type = type.replaceAll("\r|\n|\\s", "");
        LogToFile.d("====---list-====", "list:" + list);
        failureToReport(type, Constants.getStr(type), 0);
        //返回
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > 10 * 1000) {
                LogToFile.d("=====检测继续转账超时====", "=====检测继续转账超时，点击坐标===");
                mDevice.click(170, 280);
                break;
            }
            UiObject back0 = mDevice.findObject(new UiSelector().descriptionContains("继续转账").className("android.widget.Button"));
            UiObject back1 = mDevice.findObject(new UiSelector().text("继续转账").className("android.widget.Button"));
            LogToFile.d("=====查找继续转账====", "=====查找继续转账===");
            if (back0.exists()) {
                back0.click();
                LogToFile.d("=====转账完成返回====", "=====转账完成返回===");
                break;
            } else if (back1.exists()) {
                back1.click();
                LogToFile.d("=====转账完成返回====", "=====转账完成返回===");
                break;
            }
            sleep(1000);
            LogToFile.d("=====交易结果界面未找到<继续转账>元素，继续循环查找元素=====", "=====交易结果界面未找到<继续转账>元素，继续循环查找元素=====");
        }

        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > 10 * 1000) {
                LogToFile.d("=====检测立即转账超时====", "=====检测立即转账超时，点击坐标===");
                mDevice.click(170, 280);
                break;
            }
            LogToFile.d("=====查找立即转账=====", "=====查找立即转账=====");

            UiObject ljzz1 = mDevice.findObject(new UiSelector().text("立即转账").className("android.widget.TextView"));
            UiObject ljzz2 = mDevice.findObject(new UiSelector().descriptionContains("立即转账").className("android.widget.TextView"));
            if (ljzz1.exists()) {
                LogToFile.d("=====查找立即转账=====", "=====立即转账找到=====");
                ljzz1.click();
                break;
            } else if (ljzz2.exists()) {
                LogToFile.d("=====查找立即转账=====", "=====立即转账找到=====");
                ljzz2.click();
                break;
            }
        }
    }

    private void backTransfer() throws UiObjectNotFoundException {
        while (true) {
            UiObject uiObject = mDevice.findObject(new UiSelector().className("android.widget.TextView").text("预约转账"));
            if (uiObject.exists()) {
                break;
            } else {
                mDevice.click(75, 160);
                sleep(3000);
            }
        }
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > 10 * 1000) {
                LogToFile.d("=====检测立即转账超时====", "=====检测立即转账超时，点击坐标===");
                mDevice.click(170, 280);
                break;
            }
            LogToFile.d("=====查找立即转账=====", "=====查找立即转账=====");

            UiObject ljzz1 = mDevice.findObject(new UiSelector().text("立即转账").className("android.widget.TextView"));
            UiObject ljzz2 = mDevice.findObject(new UiSelector().descriptionContains("立即转账").className("android.widget.TextView"));
            if (ljzz1.exists()) {
                LogToFile.d("=====查找立即转账=====", "=====立即转账找到=====");
                ljzz1.click();
                break;
            } else if (ljzz2.exists()) {
                LogToFile.d("=====查找立即转账=====", "=====立即转账找到=====");
                ljzz2.click();
                break;
            }
        }
    }

    /**
     * 对抓到的弹框信息进行处理
     */
    private static boolean isTrue(String info) {
        boolean b = false;
        LogToFile.d("---错误信息判断---", "---错误信息判断---");
        if ("收款账户和付款账户不能相同".equals(info) || info.contains("不存在") || info.contains("他行返回")) {
            b = true;
        }
        return b;
    }

    /**
     * 查账
     *
     * @throws UiObjectNotFoundException 异常捕获
     */
    private void auditOrder() throws UiObjectNotFoundException {
        Exception exception = new Exception();
        StackTraceElement[] trace = exception.getStackTrace();
        String method = trace[0].getMethodName();
        //返回
        sleep(1000);
        while (true) {
            UiObject username = mDevice.findObject(new UiSelector().text("收款人姓名").className("android.widget.EditText"));
            if (username.exists()) {
                break;
            }
        }
        ChatClient.request();
        //进入转账页面
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtimewait) {
                break;
            }
            UiObject ScrollView = mDevice.findObject(new UiSelector().className("android.widget.ScrollView"));
            UiObject viewGroup1 = ScrollView.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
            UiObject viewGroup2 = viewGroup1.getChild(new UiSelector().className("android.view.ViewGroup").index(6));
            UiObject viewGroup3 = viewGroup2.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
            UiObject textView = viewGroup3.getChild(new UiSelector().className("android.widget.TextView").index(5));
            if (textView.exists()) {
                LogToFile.d("====---textView-====", "textView1:" + textView.getText());
                sumMoney = textView.getText();
                if (!sumMoney.contains("元")) {
                    sumMoney = textView.getContentDescription();
                }
                sumMoney = sumMoney.replaceAll("元", "");
                sumMoney = sumMoney.replaceAll(",", "");
                LogToFile.d("====---moeny-====", "===moeny======");
                if (!sumMoney.equals("")) {
                    C_updateBalance_W(Double.parseDouble(sumMoney));
                    sleep(1000);
                }
                break;
            }
        }
        sleep(3000);
        // 返回
        back();
        sleep(1000);
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtimewait) {
                mDevice.click(900, 355);
                break;
            }
            UiObject jyjl1 = mDevice.findObject(new UiSelector().text("交易记录").className("android.widget.TextView"));
            UiObject jyjl2 = mDevice.findObject(new UiSelector().descriptionContains("交易记录").className("android.widget.TextView"));
            if (jyjl1.exists()) {
                jyjl1.click();
                break;
            } else if (jyjl2.exists()) {
                jyjl2.click();
                break;
            }
        }

        //判断是否进入交易明细
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtime) {
                ChatClient.timer(method, ChatClient.errorStr);
                return;
            }
            UiObject zc3 = mDevice.findObject(new UiSelector().text("近一个月").className("android.widget.TextView"));
            UiObject zc4 = mDevice.findObject(new UiSelector().descriptionContains("近一个月").className("android.widget.TextView"));
            if (zc3.exists() || zc4.exists()) {
                auditOrder2();
                break;
            }
        }

        sleep(3000);
        back();
        sleep(1000);
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtimewait) {
                mDevice.click(170, 280);
                break;
            }
            UiObject ljzz1 = mDevice.findObject(new UiSelector().text("立即转账").className("android.widget.TextView"));
            UiObject ljzz2 = mDevice.findObject(new UiSelector().descriptionContains("立即转账").className("android.widget.TextView"));
            if (ljzz1.exists()) {
                ljzz1.click();
                break;
            } else if (ljzz2.exists()) {
                ljzz2.click();
                break;
            }
        }

    }

    private void auditOrder2() throws UiObjectNotFoundException {
        String money = "";
        String userN = "";
        String transactionStatus = "";
        //获取交易记录
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > 30000) {
                failureToReport("查询超时", 1, 0);
                break;
            }
            UiObject recyclerview = mDevice.findObject(new UiSelector().resourceId("com.czbank.mbank:id/recyclerview"));
            UiObject FrameLayout = recyclerview.getChild(new UiSelector().className("android.widget.FrameLayout").index(0));
            UiObject ViewGroup = FrameLayout.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
            UiObject ViewGroup1 = ViewGroup.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
            UiObject TextView = ViewGroup1.getChild(new UiSelector().className("android.widget.TextView").index(0));
            UiObject TextView1 = ViewGroup1.getChild(new UiSelector().className("android.widget.TextView").index(3));
            UiObject TextView2 = ViewGroup1.getChild(new UiSelector().className("android.widget.TextView").index(2));
            if (TextView2.exists()) {
                transactionStatus = TextView2.getText();
            }
            if (TextView.exists()) {
                userN = TextView.getText();
            }
            if (TextView1.exists()) {
                money = TextView1.getText();
                break;
            }
        }
        boolean state = false;
        if (userN.contains(list.get(0).getUserName()) && userN.contains(list.get(0).getBankNo().substring(list.get(0).getBankNo().length() - 4))
                && Constants.getStr(transactionStatus) == 0) {
            state = true;
        }
        LogToFile.d("====---money-====", "money:" + sumMoney);
        String moery1 = money.replace("-", "");
        moery1 = moery1.replaceAll(",", "");
        System.out.println(Double.parseDouble(list.get(0).getMoney()) == Double.parseDouble(moery1));
        if (Double.parseDouble(list.get(0).getMoney()) == Double.parseDouble(moery1) && state) {
            failureToReport("交易成功", 0, 1);
        } else {
            String sumMoneyFile = user.getUserName() + BANK_NAME + "_sumMoney.txt";
            String oldMoney = FileHelper.readSDFile(sumMoneyFile);
            switch (Constants.changeInAmount(BANK_NAME, oldMoney, sumMoney, list.get(0).getMoney())) {
                case "0":
                    failureToReport("交易成功", 0, 1);
                    break;
                case "1":
                    failureToReport("交易待处理...", 1, 1);
                    break;
                case "4":
                    failureToReport("未查到条目", 4, 1);
                    break;
            }
        }

    }

    /**
     * 上报内容
     *
     * @param info             上报内容
     * @param state            上报状态
     * @param auditAndTransfer 0为转账；1为查账
     */
    private void failureToReport(String info, int state, int auditAndTransfer) {
        String mobileD = "'devices':'" + getSerialNumber() + "','prot':'" + Constants.PROT + "','phoneModel':'" + BANK_NAME +
                "'," + "'info':'" + info + "'";
        String json = "'order':'" + list.get(0).getOrder() + "','balance':'" + sumMoney + "','state':" + state + "," +
                "'type':1";
        showLog("mobileD = " + mobileD);
        showLog("json = " + json);
        String requestName;
        if (auditAndTransfer == 0) {
            requestName = ChatClient.M_SEND_TRADE_RESULT_P;
        } else {
            requestName = ChatClient.S_CONFIRM_TRADE_RESULT_C;
        }
        ChatClient.msg("<YZF>{'header':'" + requestName + "'," + json + "," + mobileD + "}</YZF>");
        //删除处理过的订单
        mapDelete(list.get(0).getOrder());
        getmap();
        sleep(ChatClient.overtime_2);
    }

    /**
     * 境内汇款页面
     */
    public void remittance1() {
        try {
            sleep(1000);
            LogToFile.d(BANK_NAME, "准备获取收款人姓名View");
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime_10) {
                    LogToFile.d(BANK_NAME, "获取收款人姓名失败重新返回重新进入");
                    clickImmediatelyTransfer();
                    break;
                }
                UiObject username = mDevice.findObject(new UiSelector().text("收款人姓名").className("android.widget.EditText"));
                UiObject username1 = mDevice.findObject(new UiSelector().text("姓名").className("android.widget.TextView"));
                if (username.exists() || username1.exists()) {
                    LogToFile.d(BANK_NAME, "成功获取收款人姓名View");
                    break;
                }
            }
            //进入转账页面
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime_60) {
                    LogToFile.d(BANK_NAME, "获取余额失败");
                    break;
                }
                UiObject ScrollView = mDevice.findObject(new UiSelector().className("android.widget.ScrollView"));
                UiObject viewGroup1 = ScrollView.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
                UiObject viewGroup2 = viewGroup1.getChild(new UiSelector().className("android.view.ViewGroup").index(6));
                UiObject viewGroup3 = viewGroup2.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
                UiObject textView = viewGroup3.getChild(new UiSelector().className("android.widget.TextView").index(5));
                if (textView.exists()) {
                    LogToFile.d("====---textView-====", "textView1:" + textView.getText());
                    sumMoney = textView.getText();
                    if (!sumMoney.contains("元")) {
                        sumMoney = textView.getContentDescription();
                    }
                    sumMoney = sumMoney.replaceAll("元", "");
                    sumMoney = sumMoney.replaceAll(",", "");
                    LogToFile.d("====---moeny-====", "===moeny=======");
                    if (!sumMoney.equals("")) {
                        LogToFile.d(BANK_NAME, "本地保存余额");
                        String sumMoneyFile = user.getUserName() + BANK_FLAG + "_sumMoney.txt";
                        FileHelper.saveMoneySDFile(false, sumMoney, sumMoneyFile);
                        C_updateBalance_W(Double.parseDouble(sumMoney));
                        sleep(1000);
                    }
                    break;
                }
            }
            ChatClient.request();
            if (ChatClient.waitExistOrder(ChatClient.overtime_60)) {
                orderBankName = Constants.getBank(list.get(0).getBankName(), "CZB");
                // 转账
                showLog("进入转账");
                remittanceLongin();
                return;
            }
            sleep(5000);
            clickImmediatelyTransfer();
        } catch (UiObjectNotFoundException e) {
            LogToFile.d(BANK_NAME, "异常");
            setDevicesLog(e.getCause().getMessage());
            ChatClient.timer("remittance1", ChatClient.error);
            e.printStackTrace();
        }
    }

    private void clickImmediatelyTransfer() throws UiObjectNotFoundException {
        addWorkTime(ChatClient.overtime_20);
        // 返回
        back();
        sleep(ChatClient.overtime_1);
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        LogToFile.d(BANK_NAME, "检测到对话框");
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtime_5) {
                LogToFile.d(BANK_NAME, "未检测到对话框");
                break;
            }
            UiObject ljzz1 = mDevice.findObject(new UiSelector().resourceId("com.czbank.mbank:id/positiveButton"));
            if (ljzz1.exists()) {
                LogToFile.d(BANK_NAME, "点击确定");
                ljzz1.click();
                break;
            }
        }
        sleep(ChatClient.overtime_1);
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        LogToFile.d(BANK_NAME, "准备点击立即转账");
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtime_30) {
                LogToFile.d(BANK_NAME, "异常后点击立即转账");
                mDevice.click(170, 280);
                break;
            }
            UiObject ljzz1 = mDevice.findObject(new UiSelector().text("立即转账").className("android.widget.TextView"));
            UiObject ljzz2 = mDevice.findObject(new UiSelector().descriptionContains("立即转账").className("android.widget.TextView"));
            if (ljzz1.exists()) {
                LogToFile.d(BANK_NAME, "点击立即转账");
                ljzz1.click();
                break;
            } else if (ljzz2.exists()) {
                LogToFile.d(BANK_NAME, "点击立即转账");
                ljzz2.click();
                break;
            }
        }
    }

    /**
     * 返回
     */
    public static void back() {
        LogToFile.d(BANK_NAME, "返回");
        mDevice.click(80, 135);
    }

    /**
     * 确认转账
     *
     * @throws UiObjectNotFoundException 异常捕获
     */
    public static void setNext() throws UiObjectNotFoundException {
        while (true) {
            UiObject bankPwdU = mDevice.findObject(new UiSelector().text("确  定").className("android.widget.Button"));
            LogToFile.d("=====检测确定按钮====", "=====检测确定按钮=====");
            //判断银行密码
            if (bankPwdU.exists()) {
                LogToFile.d("====点击确定按钮====", "=====点击确定按钮=====");
                bankPwdU.click();
                break;
            }
        }
    }


    /**
     * 判断是否达到选择开户行的临界值
     *
     * @param s 金额
     * @return true
     */
    private static boolean isOverMax(String s) {
        boolean b = false;
        double c = 50000.00;//临界值为50000.00
        LogToFile.d("====判断金额是否超过填写开户行的临界值====", "===判断金额是否超过填写开户行的临界值====");
        double money = Double.parseDouble(s);
        if (money > c) {
            b = true;
        }
        LogToFile.d("====判断金额是否超过填写开户行的临界值====", "===判断金额是否超过填写开户行的临界值====" + b);
        return b;
    }

    /**
     * 账户与姓名不符
     *
     * @param info type
     *             0 -- 失败
     *             1 -- 金额不足
     *             <p>
     *             state -- 1 (失败)
     *             state -- 6 (待确认)
     * @throws UiObjectNotFoundException 非我行手机银行大额转账工作时间！
     */
    private void alertError(String info) throws UiObjectNotFoundException {
        LogToFile.d("=====账户与姓名不符====", "=====账户与姓名不符=====");
        LogToFile.d("==转账错误信息==", "=====转账错误信息====" + info);
        failureToReport(info, 1, 0);
        // 返回
        back();
        sleep(4000);
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > 10 * 1000) {
                break;
            }
            LogToFile.d("==转账失败信息==", "=====返回后查找下一步按钮====" + info);

            UiObject next = mDevice.findObject(new UiSelector().text("下一步").className("android.widget.Button"));
            sleep(1000);
            if (next.exists()) {
                LogToFile.d("==转账失败信息==", "=====返回后查找下一步按钮存在====" + info);
                break;
            }
        }

        // 返回
        back();
        LogToFile.d("==转账失败信息==", "=====返回到立即转账界面====" + info);
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtimewait) {
                mDevice.click(170, 280);
                break;
            }
            UiObject ljzz1 = mDevice.findObject(new UiSelector().text("立即转账").className("android.widget.TextView"));
            UiObject ljzz2 = mDevice.findObject(new UiSelector().descriptionContains("立即转账").className("android.widget.TextView"));
            sleep(1000);
            if (ljzz1.exists()) {
                ljzz1.click();
                break;
            } else if (ljzz2.exists()) {
                ljzz2.click();
                break;
            }
        }
    }

    private void showLog(String msg) {
        LogToFile.showLog(BANK_NAME, msg);
    }
}